<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
	/**
     * 失败
     * @return [type] [description]
     */
    function json_error($msg='失败')
    {
        return json(['code'=>400, 'msg'=>$msg]);
    }

    /**
     * 成功
     * @return [type] [description]
     */
    function json_success($data=[], $msg='成功')
    {
        if (is_string($data)) {
            $msg  = $data;
            $data = [];
        }
        return json(['code'=>200, 'data'=>$data, 'msg'=>$msg]);
    }

    /**
     * 生成唯一订单号
     * @return [type] [description]
     */
    function getOrderNum()
    {
        $num = substr(md5(time().mt_rand(11111,99999)),0,18);
        $oN = db('order')->where('order_number',$num)->find();
        if ($oN) $this->getOrderNum();
        return $num;
    }

    /**
     * 生成唯一订单号
     * @param table 表明
     * @return [type] [description]
     */
    function getOrderNumber($table)
    {
        $num = substr(md5(time().mt_rand(11111,99999)),0,18);
        $oN = db($table)->where('order_number',$num)->find();
        if ($oN) $this->getOrderNum();
        return $num;
    }

    /**
     * 发送手机验证码
     * @param int phone
     * @return [type] [description]
     */
    function sendPhoneCode($phone)
    {
    	$exTime = 60;																					// 过期时间
    	if(!$phone) return json_error('手机号不能为空');
    	if(!preg_match("/^1[3456789]\d{9}$/", $phone)) return json_error('请输入有效手机号');
    	$lastGet = db('phone_code')->where('user_phone',$phone)->order('mes_id desc')->find();
    	if ($lastGet) 
    	{
    		# 判断重复请求
    		if((explode(',',$lastGet['phone_code'])[1]+$exTime)-time() > 0) return json_error($exTime.'秒内只能请求一次');
    	}
    	$code = mt_rand(111111,999999);
    	$codeArr['user_phone'] = $phone; 
    	$codeArr['phone_code'] = $code.'zc,'.time(); 
    	$cN = db('phone_code')->insert($codeArr);
    	if(!$cN) return json_error('验证码发送失败'); 
    	sendApi($phone,$code);
    	return json_success(['pCode' => $code], '验证码发送成功');
    }

    /**
     * 请求短息接口
     * @param int phone
     * @return [type] [description]
     */
    function sendApi($phone,$code)
    {
    	$phone 		= trim($phone);
    	$content 	= urlencode(iconv("UTF-8", "gb2312//IGNORE", trim('验证码为'.$code.',有效时间5分钟！【樱桃荟】')));
    	$username 	= 'yth111';
    	$password 	= '615$(Cch';
    	$fp 		= Fopen("http://120.25.66.83:9877/api/send.aspx?username=$username&password=$password&mobiles=$phone&content=$content","r");
    	$ret 		= fgetss($fp,255);
    	$str 		= urldecode($ret);
    	fclose($fp);
    	return $str;
    }

    // 隐藏部分手机号
    function hiddenP($str)
    {
        return substr($str, 0, 3).'****'.substr($str, -4);
    }
